package net.maku.events.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.convert.UserContestQuestionConvert;
import net.maku.events.entity.UserContestQuestionEntity;
import net.maku.events.query.UserContestQuestionQuery;
import net.maku.events.vo.UserContestQuestionVO;
import net.maku.events.dao.UserContestQuestionDao;
import net.maku.events.service.UserContestQuestionService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户所答竞赛
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class UserContestQuestionServiceImpl extends BaseServiceImpl<UserContestQuestionDao, UserContestQuestionEntity> implements UserContestQuestionService {

    @Override
    public PageResult<UserContestQuestionVO> page(UserContestQuestionQuery query) {
        IPage<UserContestQuestionEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(UserContestQuestionConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<UserContestQuestionEntity> getWrapper(UserContestQuestionQuery query){
        LambdaQueryWrapper<UserContestQuestionEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(UserContestQuestionVO vo) {
        UserContestQuestionEntity entity = UserContestQuestionConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(UserContestQuestionVO vo) {
        UserContestQuestionEntity entity = UserContestQuestionConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}